﻿using System;
using System.Collections.Generic;

namespace YArchitech.Plumbing
{
	public class FitCurve : GaussMatrix
	{
		public FitCurve()
		{
			this.InitParam();
			this.m_nTimes = 2;
			this.m_efitMethod = _EFitMatrixMethod.eCompleteFill;
		}

		public FitCurve(int nTimes)
		{
			this.m_nTimes = 2;
			this.InitParam();
			this.m_efitMethod = _EFitMatrixMethod.eQuickFill;
		}

		public void InitParam()
		{
			for (int i = 0; i < this.m_dDerivedParam.Length; i++)
			{
				this.m_dDerivedParam[i] = 0.0;
			}
		}

		private void FillMatrixComplete()
		{
			int count = this.m_ptArr.Count;
			int num = this.m_nTimes + 1;
			for (int i = 1; i <= num; i++)
			{
				int j = (1 == i) ? 2 : 1;
				if (1 == i)
				{
					this.m_Ab[1, 1] = (double)this.m_ptArr.Count;
				}
				while (j <= num)
				{
					int num2 = i + j - 2;
					double num3 = 0.0;
					for (int k = 0; k < count; k++)
					{
						stCurvePt stCurvePt = this.m_ptArr[k];
						num3 += Math.Pow(stCurvePt.first, (double)num2);
					}
					this.m_Ab[i, j] = num3;
					j++;
				}
			}
			for (int l = 1; l <= num; l++)
			{
				double num4 = 0.0;
				for (int m = 0; m < count; m++)
				{
					stCurvePt stCurvePt2 = this.m_ptArr[m];
					num4 += Math.Pow(stCurvePt2.first, (double)(l - 1)) * stCurvePt2.second;
				}
				this.m_Ab[l, num + 1] = num4;
			}
		}

		private void FillMatrixQuick()
		{
			int count = this.m_ptArr.Count;
			int num = this.m_nTimes + 1;
			for (int i = 1; i <= num; i++)
			{
				stCurvePt stCurvePt = this.m_ptArr[i - 1];
				for (int j = 1; j <= num; j++)
				{
					this.m_Ab[i, j] = ((1 == j) ? 1.0 : Math.Pow(stCurvePt.first, (double)(j - 1)));
				}
			}
			for (int k = 1; k <= num; k++)
			{
				stCurvePt stCurvePt2 = this.m_ptArr[k - 1];
				this.m_Ab[k, num + 1] = stCurvePt2.second;
			}
		}

		private void FillMatrix(_EFitMatrixMethod eMethod)
		{
			if (eMethod == _EFitMatrixMethod.eCompleteFill)
			{
				this.FillMatrixComplete();
				return;
			}
			if (eMethod != _EFitMatrixMethod.eQuickFill)
			{
				this.FillMatrixComplete();
				return;
			}
			this.FillMatrixQuick();
		}

		public bool Calculation()
		{
			base.InitMatrix();
			this.InitParam();
			this.FillMatrix(this.m_efitMethod);
			if (base.GaussMatrixCal(this.m_nTimes + 1))
			{
				for (int i = 0; i < this.m_nTimes; i++)
				{
					this.m_dDerivedParam[i] = (double)(i + 1) * this.m_dResult[i + 1];
				}
				return true;
			}
			return false;
		}

		public double CalCurveValue(double dPtX)
		{
			double num = this.m_dResult[0];
			for (int i = 1; i <= this.m_nTimes; i++)
			{
				num += this.m_dResult[i] * Math.Pow(dPtX, (double)i);
			}
			return num;
		}

		public double CalDeriveValue(double dPtX)
		{
			double num = this.m_dDerivedParam[0];
			for (int i = 1; i < this.m_nTimes; i++)
			{
				num += this.m_dDerivedParam[i] * Math.Pow(dPtX, (double)i);
			}
			return num;
		}

		public string GetParamOutput()
		{
			string text = "";
			for (int i = 0; i <= this.m_nTimes; i++)
			{
				string str = this.m_dResult[i].ToString("f2");
				text += str;
			}
			return text;
		}

		public const int Max_Number = 10;

		public _EFitMatrixMethod m_efitMethod;

		public int m_nTimes;

		public List<stCurvePt> m_ptArr = new List<stCurvePt>();

		public double[] m_dDerivedParam = new double[10];
	}
}
